package com.test.antlr4.calculator;

import org.antlr.v4.runtime.tree.TerminalNode;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.common.value.qual.IntRange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.stream.IntStream;

/**
 * @className: CalculatorVisitorImpl
 * @Description:
 * @Author: wangyifei
 * @Date: 2025/5/21 17:40
 */
public class CalculatorVisitorImpl extends CalculatorBaseVisitor<Integer>{
    private static Logger logger = LoggerFactory.getLogger(CalculatorVisitorImpl.class);

    @Override
    public Integer visitMulDiv(CalculatorParser.MulDivContext ctx) {
        int a = visit(ctx.expr(0));
        int b = visit(ctx.expr(1));
        int rs = "*".equals(ctx.op.getText())? a * b : a / b ;
        return rs;
    }

    @Override
    public Integer visitAddSub(CalculatorParser.AddSubContext ctx) {

        int a = visit(ctx.expr(0));
        int b = visit(ctx.expr(1));
        int rs = "-".equals(ctx.op.getText())? a - b : a + b ;
        return rs;
    }

    @Override
    public Integer visitParens(CalculatorParser.ParensContext ctx) {
        return visit(ctx.expr()) ;
    }

    @Override
    public Integer visitNnumber(CalculatorParser.NnumberContext ctx) {
        return Integer.parseInt(ctx.getText());
    }

    @Override
    public Integer visitSumFunction(CalculatorParser.SumFunctionContext ctx) {
        Integer a = visit(ctx.expr(0));
        Integer b = visit(ctx.expr(1));
        return IntStream.rangeClosed(a , b).sum();
    }
}
